package com.example.project.dao;

import com.example.project.pojo.Car;
import com.example.project.pojo.Parking;
import com.example.project.pojo.User;
import com.example.project.pojo.WalletHistory;
import com.example.project.util.DBHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class WalletHistroyDAO {

    public void chargeHistory(User user,double money){
       Connection connection = DBHelper.getConnection();
        PreparedStatement ps =null;
        String sql ="insert into wallet_history (user_id,money,remainder_money,beizhu) values(?,?,?,?)";
        try {
            ps=connection.prepareStatement(sql);
            ps.setInt(1,user.getId());
            ps.setDouble(2,money);
            ps.setDouble(3,user.getTotalMoney()+money);
            ps.setString(4,"余额充值");
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBHelper.close(ps,connection);
        }
    }
    public void parkingHistory(Parking parking,User user,double money){
        Connection connection = DBHelper.getConnection();
        PreparedStatement ps =null;
        String sql ="insert into wallet_history (user_id,money,remainder_money,beizhu,car_id,parking_id) values(?,?,?,?,?,?)";
        try {
            ps=connection.prepareStatement(sql);
            ps.setInt(1,user.getId());
            ps.setDouble(2,money);
            ps.setDouble(3,user.getTotalMoney()-money);
            ps.setString(4,"车位缴费");
            ps.setInt(5,parking.getCar().getId());
            ps.setInt(6,parking.getId());
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBHelper.close(ps,connection);
        }
    }
    public List<WalletHistory> findChargeHistoryByUserID(User user){
        Connection connection = DBHelper.getConnection();
        PreparedStatement ps =null;
        ResultSet rs =null;
        List<WalletHistory> walletHistoryList =new ArrayList<>();
        String sql ="select * from wallet_history where user_id = ? and beizhu = ?";
        try {
            ps=connection.prepareStatement(sql);
            ps.setInt(1,user.getId());
            ps.setString(2,"余额充值");
            rs= ps.executeQuery();
            while (rs.next()){
                WalletHistory walletHistory =new WalletHistory();
                walletHistory.setUser(user);
                walletHistory.setTime(rs.getTimestamp("time"));
                walletHistory.setMoney(rs.getDouble("money"));
                walletHistory.setBeiZhu("余额充值");
                walletHistory.setRemainderMoney(rs.getDouble("remainder_money"));
                walletHistoryList.add(walletHistory);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBHelper.close(rs,ps,connection);
        }
        return walletHistoryList;
    }
    public List<WalletHistory> findParkingHistoryByUserID(User user){
        Connection connection = DBHelper.getConnection();
        PreparedStatement ps =null;
        ResultSet rs =null;
        List<WalletHistory> walletHistoryList =new ArrayList<>();
        String sql ="select * from wallet_history where user_id = ? and beizhu=?";

        ParkingDAO parkingDAO =new ParkingDAO();
        CarDAO carDAO =new CarDAO();
        try {
            ps=connection.prepareStatement(sql);
            ps.setInt(1,user.getId());
            ps.setString(2,"车位缴费");
            rs= ps.executeQuery();
            while (rs.next()){
                WalletHistory walletHistory =new WalletHistory();
                walletHistory.setUser(user);
                walletHistory.setParking(parkingDAO.findParkingByID(rs.getInt("parking_id")));
                walletHistory.setCar(carDAO.findCarByID(rs.getInt("car_id")));
                walletHistory.setTime(rs.getTimestamp("time"));
                walletHistory.setMoney(rs.getDouble("money"));
                walletHistory.setBeiZhu("车位缴费");
                walletHistory.setRemainderMoney(rs.getDouble("remainder_money"));
                walletHistoryList.add(walletHistory);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DBHelper.close(rs,ps,connection);
        }
        return walletHistoryList;
    }
}
